import {
  getListService,
  addOneFieldService,
  deleteOneFieldService,
  updateOneFieldService,
  addOneRecordService,
  deleteOneRecordService,
} from '@/services/zhongzhijilu';
import { notiErr } from '@/utils/notification';
import { getNongjizulinListService } from '@/services/nongjizulin';

const initialState = {
  listData: [],
};

export default {
  namespace: 'zhongzhiguanli_zhongzhijilu',

  state: {
    ...initialState,
  },

  effects: {
    * getListEffect(_, { call, put, select }) {
      const token = yield select(({ app }) => app.user.token);
      const response = yield call(getListService, { token });

      if (response && !response.errno && response.data) {
        const rdata = response.data;
        yield put({
          type: 'getListReducer',
          payload: {
            listData: rdata,
          },
        });
      } else {
        notiErr(response.errmsg);
      }
    },
    // 添加新土地
    * addNewFieldEffect({ payload: { data } }, { call, put, select }) {
      const token = yield select(({ app }) => app.user.token);
      const response = yield call(addOneFieldService, {
        token,
        area: data.area,
        fieldName: data.fieldName,
      });

      if (response && !response.errno && response.data) {
        const rdata = response.data;
        yield put({
          type: 'addNewFieldReducer',
          payload: {
            fieldId: rdata.id,
            area: data.area,
            fieldName: data.fieldName,
          },
        });
      } else {
        notiErr(response.errmsg);
      }
    },
    // 删除土地
    * deleteFieldEffect({ payload: { data } }, { call, put, select }) {
      const token = yield select(({ app }) => app.user.token);
      const response = yield call(deleteOneFieldService, {
        token,
        fid: data.fid,
      });

      if (response && !response.errno) {
        yield put({
          type: 'deleteFieldReducer',
          payload: {
            fid: data.fid,
          },
        });
      } else {
        notiErr(response.errmsg);
      }
    },
    // 更改土地名
    * updateFieldEffect({ payload: { data } }, { call, put, select }) {
      const token = yield select(({ app }) => app.user.token);
      const response = yield call(updateOneFieldService, {
        token,
        fid: data.fid,
        fieldName: data.fieldName,
        area: data.area,
      });

      if (response && !response.errno) {
        yield put({
          type: 'updateFieldReducer',
          payload: {
            fid: data.fid,
            fieldName: data.fieldName,
            area: data.area,
          },
        });
      } else {
        notiErr(response.errmsg);
      }
    },

    // 添加新记录
    * addNewRecordEffect({ payload: { data } }, { call, put, select }) {
      console.log(data);
      const token = yield select(({ app }) => app.user.token);
      const response = yield call(addOneRecordService, {
        token,
        fid: data.fieldId,
        bingchongfangzhiCost: data.bingchongfangzhi_cost,
        bingchongfangzhiTime: data.bingchongfangzhi_time,
        bozhongCost: data.bozhong_cost,
        bozhongTime: data.bozhong_time,
        miaochuangzhengdiCost: data.miaochuangzhengdi_cost,
        miaochuangzhengdiTime: data.miaochuangzhengdi_time,
        otherTime: data.other_time,
        otherName: data.other_name,
        otherCost: data.other_cost,
        shifeiCost: data.shifei_cost,
        shifeiTime: data.shifei_time,
        shougeCost: data.shouge_cost,
        shougeTime: data.shouge_time,
      });

      if (response && !response.errno && response.data) {
        const rdata = response.data;
        yield put({
          type: 'addNewRecordReducer',
          payload: {
            ...data,
            id: rdata.id,
          },
        });
      } else {
        notiErr(response.errmsg);
      }
    },
    // 删除一条记录
    * deleteRecordEffect({ payload: { data } }, { call, put, select }) {
      const token = yield select(({ app }) => app.user.token);
      const response = yield call(deleteOneRecordService, {
        token,
        rid: data.id,
      });

      if (response && !response.errno) {
        yield put({
          type: 'deleteRecordReducer',
          payload: {
            id: data.id,
          },
        });
      } else {
        notiErr(response.errmsg);
      }
    },
  },

  reducers: {
    // 删除土地的不数据处理逻辑
    deleteFieldReducer(state, { payload }) {
      const oldList = state.listData;
      const newList = [];
      for(let oneFieldRecord of oldList) {
        if(payload.fid !== oneFieldRecord[0].fieldId) {
          newList.push(oneFieldRecord);
        }
      }

      return {
        ...state,
        listData: newList,
      };
    },

    updateFieldReducer(state, { payload }) {
      const oldList = state.listData;
      const newList = [];
      for(let oneFieldRecord of oldList) {
        newList.push(oneFieldRecord.map(v => ({
          ...v,
          fieldName: v.fieldId === payload.fid ? payload.fieldName : v.fieldName,
          area: v.fieldId === payload.fid ? payload.area : v.area,
        })));
      }

      return {
        ...state,
        listData: newList,
      };
    },
    getListReducer(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
    addNewFieldReducer(state, { payload }) {
      const newList = [...state.listData, [{
        id: Date.now(),
        fieldId: payload.fieldId,
        fieldName: payload.fieldName,
        area: payload.area,
        noRecord: true, // 没有记录，只有土地名字
      }]];

      return {
        ...state,
        listData: newList,
      };
    },
    addNewRecordReducer(state, { payload }) {
      const oldList = state.listData;
      const newList = [];
      for(let oneFieldRecord of oldList) {
        if(oneFieldRecord[0].fieldId === payload.fieldId) {
          if(oneFieldRecord[0].noRecord) {
            newList.push([{
              ...oneFieldRecord[0],
              ...payload,
              noRecord: false,
            }]);
          } else {
            newList.push([...oneFieldRecord, payload]);
          }
        } else {
          newList.push(oneFieldRecord);
        }
      }

      return {
        ...state,
        listData: newList,
      };
    },
    deleteRecordReducer(state, { payload }) {
      const oldList = state.listData;
      const newList = [];
      for(let oneFieldRecord of oldList) {
        // 删除只有一条记录的
        if(oneFieldRecord[0].id === payload.id && oneFieldRecord.length === 1) {
            newList.push([{
              id: oneFieldRecord[0].id,
              fieldId: oneFieldRecord[0].fieldId,
              fieldName: oneFieldRecord[0].fieldName,
              area: oneFieldRecord[0].area,
              noRecord: true,
            }]);
        } else {
          newList.push(oneFieldRecord.filter(v => v.id !== payload.id));
        }
      }

      return {
        ...state,
        listData: newList,
      };
    },
    resetDataReducer() {
      return {
        ...initialState,
      };
    },
  },
};
